
import sys

from subprocess import Popen, PIPE

class FZF:
    def __init__(self, exec_fzf: str = "fzf"):
        self.exec_fzf = exec_fzf
    
    def _SysCallFZF(self, data):
        if not data:
            return None
        
        data = "\n".join( list(map(str, data)) )
        
        sys_fzf = Popen( [self.exec_fzf],
                        stdin=PIPE,
                        stdout=PIPE,
                        stderr=PIPE,
                        text=True
                        )
        
        output_data, error = sys_fzf.communicate(input=data)
        
        if not error:
            return output_data.strip()
        else:
            print(f"FZF Error: {error}")
            sys.exit()
    
    def CHOISE(self, list_: list) -> str:
        return self._SysCallFZF(list_)

